package com.mutou.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.mutou.domian.model.Article;
import com.mutou.service.ArticleService;

/**
 * 文章信息
 * @author mutou
 *
 */
@RequestMapping(value="article")
@Controller
public class ArticleController {

	@Autowired
	private ArticleService articleService;
	

	/**
	 * 保存
	 * @param article
	 * @return
	 */
	@RequestMapping(value="save",method={RequestMethod.POST})
	@ResponseBody
	public String save(@ModelAttribute Article article){
		Date date = new Date();
		article.setUpdateTime(date);
		if(article!=null && article.getId()<=0){				
			article.setCreateTime(date);
			articleService.save(article);
		}else{
			articleService.update(article);
		}
		return  JSONObject.toJSONString("ok");
	}
	
	/**
	 * 列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value="list")
	@ResponseBody
	public Map<String,Object> list(@RequestParam(value="type",required=false,defaultValue="0") int type,@RequestParam("currentPage") int currentPage,@RequestParam("limit") int limit){
		PageBounds pageInfo = new PageBounds();
		pageInfo.setPage(currentPage);
		pageInfo.setLimit(limit);
		Map<String, Object> param = new HashMap<>();
		if(type>0){
			param.put("type", type);			
		}
		List<Article> list = articleService.listByPage(param,pageInfo);
		PageList<Article> pagelist = (PageList<Article>) list;
		Map<String, Object> map = new HashMap<>();
		map.put("aaData", list); //数据项
		map.put("iTotalDisplayRecords", pagelist.getPaginator().getTotalCount()); //数据项
		map.put("iTotalRecords",  pagelist.getPaginator().getTotalCount()); //数据项
		return map;
	}
	
	@RequestMapping(value="pre")
	public String webedit(){
		
		return "detail";
	}
	
	/**
	 * 根据ID获取信息
	 * @return
	 */
	@RequestMapping("pre/{id}")
	public ModelAndView preUpate(@PathVariable("id")int id){	
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("id", id);
		modelAndView.setViewName("detail");
		return modelAndView;
	}
	@RequestMapping("{id}")
	@ResponseBody
	public Article queryEntById(@PathVariable("id")int id){	
		Article article = articleService.queryEntById(id);
		return article;
	}
	
	
}
